package com.bfly.mall.order.event;

import com.bfly.mall.order.entity.Order;
import com.bfly.mall.order.entity.OrderSku;
import com.bfly.mall.order.service.IOrderService;
import com.bfly.mall.user.entity.User;
import com.bfly.mall.wechat.minipro.service.WeChatMiniProAPi;
import com.bfly.mall.wechat.minipro.dto.message.WeChatCancelSuccessMessage;
import com.bfly.mall.wechat.minipro.dto.message.WeChatPaySuccessMessage;
import com.bfly.mall.wechat.minipro.dto.message.WeChatRefundSuccessMessage;
import com.bfly.mall.wechat.minipro.dto.message.WeChatShipSuccessMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;

import java.util.Date;
import java.util.List;

/**
 * 订单时间监听器
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/12/14 21:45
 */
@Configuration
public class OrderEventListener {

    private Logger logger = LoggerFactory.getLogger(OrderEventListener.class);

    /**
     * 订单支付成功事件
     *
     * @author 长沙商脉科技有限公司
     * @date 2019/8/18 15:41
     */
    @EventListener
    @Async
    public void orderPayEvent(OrderPayEvent event) {
        ApplicationContext context = event.getApplicationContext();
        IOrderService orderService = context.getBean(IOrderService.class);

        Order order = orderService.getOrder(event.getOrderNo());
        User user = order.getUser();
        String openId = user.getOpenId();
        WeChatPaySuccessMessage message = new WeChatPaySuccessMessage();
        message.setTouser(openId);
        message.setBookingTime(order.getCreateDate());
        message.setPayTime(event.getPayTime());
        message.setPrice(order.getPayPrice());
        message.setOrderNo(order.getOrderNo());
        message.setPage("/pages/home/order/detail?orderNo=" + order.getOrderNo());

        StringBuilder sb = new StringBuilder();
        List<OrderSku> skus = order.getSkus();
        skus.forEach(sku -> {
            sb.append(sku.getProductName() + "、");
        });
        message.setProductName(sb.toString());
        WeChatMiniProAPi.sendSubscribeMessage(message);
    }

    /**
     * 订单取消事件
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/12/14 21:57
     */
    @EventListener
    @Async
    public void orderCancelEvent(OrderCancelEvent event) {
        ApplicationContext context = event.getApplicationContext();
        IOrderService orderService = context.getBean(IOrderService.class);

        Order order = orderService.getOrder(event.getOrderNo());
        User user = order.getUser();
        String openId = user.getOpenId();
        WeChatCancelSuccessMessage message = new WeChatCancelSuccessMessage();
        message.setOrderNo(order.getOrderNo());
        message.setTouser(openId);
        message.setTime(new Date());
        message.setReason(event.getReason());
        message.setPage("/pages/home/order/detail?orderNo=" + order.getOrderNo());

        StringBuilder sb = new StringBuilder();
        List<OrderSku> skus = order.getSkus();
        skus.forEach(sku -> {
            sb.append(sku.getProductName() + "、");
        });
        message.setProductName(sb.toString());
        WeChatMiniProAPi.sendSubscribeMessage(message);
    }


    /**
     * 订单退款事件
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/12/14 22:11
     */
    @EventListener
    @Async
    public void orderRefundEvent(OrderRefundEvent event) {
        ApplicationContext context = event.getApplicationContext();
        IOrderService orderService = context.getBean(IOrderService.class);

        Order order = orderService.getOrder(event.getOrderNo());
        User user = order.getUser();
        String openId = user.getOpenId();
        WeChatRefundSuccessMessage successMessage = new WeChatRefundSuccessMessage();
        successMessage.setTouser(openId);
        successMessage.setPage("/pages/home/order/detail?orderNo=" + order.getOrderNo());
        successMessage.setOrderNo(order.getOrderNo());

        successMessage.setRefundTime(event.getRefundSuccessTime());
        successMessage.setPrice(event.getRefundPrice());
        successMessage.setStatus("退款成功");

        StringBuilder sb = new StringBuilder();
        List<OrderSku> skus = order.getSkus();
        skus.forEach(sku -> {
            sb.append(sku.getProductName() + "、");
        });
        successMessage.setProductName(sb.toString());
        WeChatMiniProAPi.sendSubscribeMessage(successMessage);
    }

    /**
     * 订单发货事件
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/12/14 22:11
     */
    @EventListener
    @Async
    public void orderShipEvent(OrderShipEvent event) {
        ApplicationContext context = event.getApplicationContext();
        IOrderService orderService = context.getBean(IOrderService.class);

        Order order = orderService.getOrder(event.getOrderNo());
        User user = order.getUser();
        String openId = user.getOpenId();
        WeChatShipSuccessMessage successMessage = new WeChatShipSuccessMessage();
        successMessage.setTouser(openId);
        successMessage.setPage("/pages/home/order/detail?orderNo=" + order.getOrderNo());
        successMessage.setOrderNo(order.getOrderNo());

        successMessage.setTime(event.getTime());
        successMessage.setExpressName(event.getExpressName());
        successMessage.setExpressNo(event.getExpressNo());

        StringBuilder sb = new StringBuilder();
        List<OrderSku> skus = order.getSkus();
        skus.forEach(sku -> {
            sb.append(sku.getProductName() + "、");
        });
        successMessage.setProductName(sb.toString());
        WeChatMiniProAPi.sendSubscribeMessage(successMessage);
    }
}
